<?php
$id			= intval(mosGetParam($_REQUEST, 'id', 0));
//path image
$path = $mosConfig_live_site."/uploads/news";

//get user
$sql = "SELECT sectionid,catid FROM #__news WHERE id=".$id;
$database->setQuery($sql);
$database->loadObject($thisUser);

$sql = "SELECT * FROM #__news WHERE state=1 AND id!=".$id." AND (catid=".$thisUser->catid." OR sectionid=".$thisUser->sectionid.")";
$database->setQuery($sql);
$rows = $database->loadObjectList();

$nows = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
$arr = "(";
$notArr = "(";
for ($i=0; $i<count($rows); $i++){
	if ($i == (count($rows)-1) || count($rows)==1) {
		$str = "";
	}else{
		$str = ",";
	}
	$end_date = mktime(unFormatdate($rows[$i]->end_date,"H"),unFormatdate($rows[$i]->end_date,"i"),unFormatdate($rows[$i]->end_date,"s"),unFormatdate($rows[$i]->end_date,"m"),unFormatdate($rows[$i]->end_date,"d"),unFormatdate($rows[$i]->end_date,"Y"));				
	$days = ($end_date - $nows)/86400;
	if ($days>0){
		$notArr .= "";
	}else{
		$notArr .= $rows[$i]->id.$str;
	}
}
$notArr .= ")";

//set state in notArr unpublish
$bodytag = str_replace("(", "", $notArr);
$bodytag = str_replace(")", "", $bodytag);
if($bodytag != '') mysql_query("UPDATE ".$mosConfig_dbprefix."news SET state=0 WHERE id IN".$notArr); 

//other news of catid
$sql = "SELECT * FROM #__news WHERE state=1 AND id!=".$id." AND (catid=".$thisUser->catid." OR sectionid=".$thisUser->sectionid.") ORDER BY created DESC, id DESC";
$database->setQuery($sql);
$lsNewsOthersCatID = $database->loadObjectList();

if (count($lsNewsOthersCatID)>0){
?>
<div class="w_boxright clearfix">
	<div class="w_title_boxright"><span>Các khuyến mại cùng loại khác</span></div>
	<?php
	//show khuyen mai theo catid
	for ($i=0; $i<count($lsNewsOthersCatID); $i++){
	$link = sefRelToAbs("index.php?option=com_news&task=view&id=".$lsNewsOthersCatID[$i]->id).$lsNewsOthersCatID[$i]->title_alias;	
	if ($i == (count($lsNewsOthersCatID)-1))	$styleBorder = ' style="border-bottom:0;"';
	else $styleBorder = '';
	
	//Tinh thoi gian
	$starttime=time();
	$endtime=strtotime($lsNewsOthersCatID[$i]->end_date);
	 
	$timediff = $endtime-$starttime;
	   $days=intval($timediff/86400);
	   $remain=$timediff%86400;
	   $hours=intval($remain/3600);
	   $remain=$remain%3600;
	   $mins=intval($remain/60);
	   $secs=$remain%60;
	//$timediff=$days.'days'.$hours.'hr'.$mins.'min'.$secs.'s';   
	?>
	<div class="w_news"<?php echo $styleBorder;?>>
		<a href="<?php echo $link;?>" class="screenshot" rel="<?php echo $path;?>/medium/<?php echo $lsNewsOthersCatID[$i]->mediumImage;?>">
			<img src="<?php echo $path;?>/thumbnail/<?php echo $lsNewsOthersCatID[$i]->thumbnailImage;?>" width="80" alt="" border="0" />
		</a>
		<a href="<?php echo $link;?>"><?php echo $lsNewsOthersCatID[$i]->title;?></a>
		<p><?php echo mb_substr($lsNewsOthersCatID[$i]->introtext, 0, 150, "utf-8")."...";?></p>
		<p class="datetime">Ngày bắt đầu: <?php echo unFormatDate($lsNewsOthersCatID[$i]->start_date,"d/m/Y H:i:s");?></p>
		<p class="datetime">Ngày kết thúc: <?php echo unFormatDate($lsNewsOthersCatID[$i]->end_date,"d/m/Y H:i:s");?></p>
		<div>
			<span>Thời gian còn lại: </span>
			<span class="clearfix"></span>
			<span class="timer"><?php echo $timediff=$days.' ngày, '.$hours.' giờ, '.$mins.' phút';?></span>
			<span class="clearfix"></span>
		</div>
	</div>
	<div class="clearfix"></div>
	<?php }?>
</div>
<?php }?>